/**
 * Created with JetBrains WebStorm.
 * User: Chetan
 * Date: 13/5/13
 * Time: 10:56 AM
 * To change this template use File | Settings | File Templates.
 */

describe("ProfileListController",function() {
    var profilesListController = null;
    ProductProfileListModel = Ember.Object.extend({
        currencyCode: BreezeConfig.currencyCode,
        title: 'Unknown',
        list:[],
        hasItems:function(){
            return (this.list.length !== 0);
        }.property('list').volatile()
    });
    var productListTemp = {
        iHave:{
            list:[
                {},
                {}
            ]
        },
        iOwe:{
            list:[
                {},
                {}
            ]
        }
    };

    beforeEach(function() {
        var t_appCtrl = App.ApplicationController.create();
        var t_alertCtrl = App.AlertController.create();
        var t_loadingCtrl = App.LoadingController.create();

        App.ProfileListController.reopen({
            init:function(){
                this.set('controllers.application', t_appCtrl);
                this.set('controllers.alert', t_alertCtrl);
                this.set('controllers.loading', t_loadingCtrl);
            }
        });

        profilesListController = App.ProfileListController.create();
        profilesListController.set("iHaveListObj",ProductProfileListModel.create({title:"I Have", elementId: 'i-have',list:[]}));
        profilesListController.set("iOweListObj",ProductProfileListModel.create({title:"I Owe", elementId: 'i-owe',list:[]}));
        profilesListController.set("productTypes",productListTemp);
    });

    it("prepareController",function() {
        spyOn(profilesListController,"productTypes");
        spyOn(profilesListController,"populateProducts");
        profilesListController.prepareController();
        expect(profilesListController.populateProducts).toHaveBeenCalled();
    });

    it("populateProducts if",function() {
        App.getSession().setProductList(productListTemp);
        profilesListController.populateProducts();
        expect(profilesListController.iHaveListObj.list.length).toBe(2);
        expect(profilesListController.iOweListObj.list.length).toBe(2);
    });

    it("populateProducts else",function() {
        var productList = {
            iHave:{
                list:[]
            },
            iOwe:{
                list:[]
            }
        };
        App.getSession().setProductList(productList);
        profilesListController.populateProducts();
        expect(profilesListController.iHaveListObj.list.length).toBe(0);
        expect(profilesListController.iOweListObj.list.length).toBe(0);
    });


    xit("resetEditing",function() {
        spyOn(profilesListController,"doneEditing");
        profilesListController.set("itemEditing",Ember.Object.create({
            account:{description:"",updatedDescription:""}
        }));
        profilesListController.resetEditing();
        expect(profilesListController.doneEditing).toHaveBeenCalled();
    });

    xit("editItem",function() {
        spyOn(profilesListController,"populateIcons");
        spyOn(profilesListController,"set");
        profilesListController.set("controllers",
            Ember.Controller.create({
                    navbar:{
                        showCancel:function(){}
                    }
                }
            ));
        var item = Ember.Object.create({
            account:null
        });
        profilesListController.editItem(item);
        expect(profilesListController.set).toHaveBeenCalledWith('itemEditing', item);
        expect(profilesListController.set).toHaveBeenCalledWith('screenOnEditMode', true);
    });

    it("saveItem if",function() {
        spyOn(profilesListController,"resetEditing");
        spyOn(App.apiProfiles,"addOrUpdateAccounts").andCallFake(function(item,callback) {
            callback({});
        });
        var obj = Em.Object.create({
            isModified:function() {
                    return true;
            },
            updatedDescription: '123',
            account:Em.Object.create({accountDescription:'nick name'})
        });
        spyOn(profilesListController,"updateCallBack");
        profilesListController.saveItem(obj);
        expect(profilesListController.updateCallBack).toHaveBeenCalledWith({});
        expect(profilesListController.resetEditing).not.toHaveBeenCalled();
    });

    xit("selectIcon",function() {
        profilesListController.set("itemEditing",Em.Object.create({updatedIcon:""}));
        var obj = {"id":""};
        spyOn(profilesListController.iconList,"set");

        profilesListController.set("iconList",[
            {selected:true}
        ]);
        profilesListController.selectIcon(obj);
        expect(profilesListController.iconList.set).toHaveBeenCalledWith("selected",true);
    });

    it("saveItem else",function() {
        spyOn(profilesListController,"resetEditing");
        var obj = Em.Object.create({
            isModified:function() {
                return false;
            }
        });
        profilesListController.saveItem(obj);
        expect(profilesListController.resetEditing).toHaveBeenCalled();
    });

    xit("doneEditing",function() {
        profilesListController.set("itemEditing",Em.Object.create({onEditMode:false}));
        spyOn(profilesListController,"set");
        profilesListController.set("controllers",
            Ember.Controller.create({
                navbar:{
                    showLogout:function(){}
                },
                security:{}
            }
            ));
        profilesListController.doneEditing();
        expect(profilesListController.set).toHaveBeenCalledWith('screenOnEditMode', false);
    });

    it("updateCallBack",function() {
        spyOn(App.api_accounts,"productList").andCallFake(function(callback) {
            callback({});
        });
        spyOn(profilesListController,"callbackProductList");
        profilesListController.updateCallBack({});
        expect(profilesListController.callbackProductList).toHaveBeenCalledWith({});
    });

    it("callbackProductList if",function() {
        spyOn(profilesListController,"doneEditing");
        spyOn(profilesListController,"populateProducts");
        spyOn(profilesListController,"hasError").andReturn(false);
        profilesListController.callbackProductList({});
        expect(profilesListController.doneEditing).toHaveBeenCalled();
        expect(profilesListController.populateProducts).toHaveBeenCalled();
    });

    it("callbackProductList else",function() {
        spyOn(profilesListController,"doneEditing");
        spyOn(profilesListController,"populateProducts");
        spyOn(profilesListController,"hasError").andReturn(true);
        profilesListController.callbackProductList({});
        expect(profilesListController.doneEditing).toHaveBeenCalled();
        expect(profilesListController.populateProducts).not.toHaveBeenCalled();
    });

    it("cancelAction",function() {
        spyOn(profilesListController,"resetEditing");
        profilesListController.cancelAction(profilesListController);
        expect(profilesListController.resetEditing).toHaveBeenCalled();
    });
});